//随机数作为矩阵输入
#include <iostream>
#include <cstdlib>//exit，atio
#include <random>//随机数
#include <chrono>//高精度计时
#include <algorithm>
using namespace std;

/** 
 * To stop the program abnormally.
 * 
 * @param error_code code for error cases.
 */
void abort(int error_code)
{
    switch (error_code)
    {
    case 1:
	cout << "Need 1 argument." << endl;
	break;
    default:
	break;
    }
    exit(-1);
};

int main(int argc, char *argv[])//主函数/命令行参数，argc为个数，首个为可执行自己
{
    /// Set an internal clock to generator a random seed.
    typedef chrono::high_resolution_clock myclock;//毫秒为单位
    myclock::time_point beginning = myclock::now();

    /// The first main argument is the dimension of the system.
    if (argc < 2)
	abort(1);
    int n = atoi(argv[1]);//std内置函数，字符串转为int

    double A[n][n];
    double b[n];

    /// Generate a random seed.
    myclock::duration d = myclock::now() - beginning;
    unsigned seed = d.count();

    /// Setup the random generator.
    default_random_engine generator(seed);
    uniform_real_distribution<double> distribution(0.0,1.0);//01分布

    /// Generate a linear system with the given dimension n randomly.
    for (int i = 0; i < n; i++)
	for (int j = 0; j < n; j++)
	    A[i][j] = distribution(generator);
    for (int i = 0; i < n; i++)
	b[i] = distribution(generator);

    /// Output the linear system data for the next program.
    /// Set the output format and precision for the double accuracy.
    cout << n << endl;
    cout.setf(ios::fixed);//固定位数
    cout.precision(20);//输出位数
    for (int i = 0; i < n; i++)
    {
	for (int j = 0; j < n; j++)
	    cout << A[i][j] << "\t";
	cout << endl;
    }
    for (int i = 0; i < n; i++)
	cout << b[i] << "\t";
    cout << endl;
    return 0;
};
